package com.zxl.grocery.microservice.auth.server.controller;

import com.zxl.grocery.microservice.auth.server.service.AuthService;
import com.zxl.grocery.microservice.auth.server.util.user.JwtAuthenticationRequest;
import com.zxl.grocery.microservice.auth.server.util.user.JwtAuthenticationResponse;
//import com.zxl.grocery.microservice.auth.server.vo.FrontUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: zxl
 * @Description: 用户和服务之间的鉴权
 * @Date: Created in 19:58 2017/10/14
 * @Modified By:
 */

@RestController
@RequestMapping("jwt")
public class AuthController {
    @Value("${jwt.token-header}")
    private String tokenHeader;

    @Autowired
    private AuthService authService;

    /**
     * @Author: Lzx
     * @Description: 登录
     * @Params:  * @param null
     * @Date: 10:22 2017/10/19
     */
    @RequestMapping(value = "token", method = RequestMethod.POST)
    public ResponseEntity<?> createAuthenticationToken(
            @RequestBody JwtAuthenticationRequest authenticationRequest) throws Exception {
//        1、校验用户合法性 2、合法则生成token 3、返回null
        final String token = authService.login(authenticationRequest.getUsername(), authenticationRequest.getPassword());
        return ResponseEntity.ok(new JwtAuthenticationResponse(token));
    }

    /**
     * @Author: Lzx
     * @Description: 刷新token
     * @Params:  * @param null
     * @Date: 10:27 2017/10/19
     */
    @RequestMapping(value = "refresh", method = RequestMethod.GET)
    public ResponseEntity<?> refreshAndGetAuthenticationToken(
            HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);//从请求头中获取token
        String refreshedToken = authService.refresh(token);//刷新token，暂未实现
        if(refreshedToken == null) {
            return ResponseEntity.badRequest().body(null);
        } else {
            return ResponseEntity.ok(new JwtAuthenticationResponse(refreshedToken));
        }
    }

    /**
     * @Author: Lzx
     * @Description: 获取token中的存放的信息
     * @Params:  * @param token
     * @Date: 10:30 2017/10/19
     */
    @RequestMapping(value = "verify", method = RequestMethod.GET)
    public ResponseEntity<?> verify(String token) throws Exception {
//        authService.validate(token);//获取token中的用户信息
        return ResponseEntity.ok(authService.validate(token));
    }

    /**
     * @Author: Lzx
     * @Description: 注销token
     * @Params:  * @param null
     * @Date: 12:04 2017/10/19
     */
    @RequestMapping(value = "invalid", method = RequestMethod.POST)
    public ResponseEntity<?> invalid(/*@RequestHeader("access-token")*/ String token){
        authService.invalid(token); //注销token
        return ResponseEntity.ok(true);
    }

//    /**
//     * @Author: Lzx
//     * @Description: 获取用户信息对象
//     * @Params:  * @param null
//     * @Date: 12:06 2017/10/19
//     */
//    @RequestMapping(value = "user", method = RequestMethod.GET)
//    public ResponseEntity<?> getUserInfo(String token) throws Exception {
//        FrontUser userInfo = authService.getUserInfo(token);
//        if(userInfo==null) {
//            return ResponseEntity.status(401).body(false);
//        } else {
//            return ResponseEntity.ok(userInfo);
//        }
//    }
}
